package com.panda.game.xls.config;

import com.alibaba.fastjson.JSONObject;
import com.panda.game.xls.constant.ScreenConstant;

import java.io.*;

/**
 * IO流文件读取解析工具类
 */
public class IOFileLoadRunner {

    private static IOFileLoadRunner loadPathObject = new IOFileLoadRunner();

    //设置json文件内容
    protected static void setExtJarJsonFile(String jsonFileName, JSONObject jsonObject) {
        //该方式，读取jar包同级的文件
        File file = new File(ScreenConstant.DATAFOLDER_PATH, jsonFileName);
        if (!file.exists()) {
            return;
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(file);
            out.write(jsonObject.toJSONString());
            out.println();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    //读取jar外json文件
    protected static JSONObject loadExtJarJsonFile(String jsonFileName) {
        JSONObject json = null;
        BufferedReader br = null;

        try {
            //该方式，读取jar包同级的文件
            File file = new File(ScreenConstant.DATAFOLDER_PATH, jsonFileName);
            if (!file.exists()) {
                file.createNewFile();//没有该文件的话，就创建
                return null;
            }

            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            json = JSONObject.parseObject(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return json;
        }
    }

    //读取json文件，并生成json对象
    protected static JSONObject loadJsonFile(String jsonFileName) {
        JSONObject json = null;
        //该方式，打成jar包时，无法读取到文件
//        String path = loadPathObject.getClass().getResource("").getPath();
//        String path = "";
//        File file = new File(path + jsonFileName);
//        if (!file.exists()) {
//            return null;
//        }
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(loadPathObject.getClass()
                    .getClassLoader()
                    .getResourceAsStream(jsonFileName), "utf-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            json = JSONObject.parseObject(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return json;
        }
    }

}
